package com.youchuang.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.youchuang.bean.CategoryProduct;
import com.youchuang.common.HibernateConnection;
import com.youchuang.dao.CategoryProductDAO;

@Repository("categoryProductDAO")
public class CategoryProductDAOImpl implements CategoryProductDAO {

	private static final String HQL_FIND_BY_PRODUCT_ID = "from CategoryProduct where productId = :productId";
	private static final String HQL_FIND_COUNT_BY_CATEGORY_ID = "SELECT COUNT(c.id) from CategoryProduct c where categoryId = :categoryId";
	private static final String HQL_UPDAT_CATEGORY_ID = "update CategoryProduct c set c.categoryId=:content where c.categoryId=:categoryId";

	@Resource
	private HibernateConnection<CategoryProduct> hibernateConnection;

	@Override
	public void save(CategoryProduct t) {
		hibernateConnection.save(t);
	}

	@Override
	public void delete(CategoryProduct t) {
		//Do nothing because of it unusual
	}

	@Override
	public int update(CategoryProduct t) {
		//Do nothing because of it unusual
		return 0;
	}

	@Override
	public List<CategoryProduct> queryAll() {
		return null;
	}

	@Override
	public List<CategoryProduct> queryByProductId(String productId) {
		Map<String, Object> params = new HashMap<>();
		params.put("productId", productId);
		return hibernateConnection.find(HQL_FIND_BY_PRODUCT_ID, params);
	}

	@Override
	public int findCountByCategoryId(String categoryId) {
		Map<String, Object> params = new HashMap<>();
		params.put("categoryId", categoryId);
		return hibernateConnection.count(HQL_FIND_COUNT_BY_CATEGORY_ID, params).intValue();
	}

	@Override
	public void updateCategoryId(String categoryId, String content) {
		Map<String, Object> params = new HashMap<>();
		params.put("categoryId", categoryId);
		params.put("content", content);
		hibernateConnection.executeHql(HQL_UPDAT_CATEGORY_ID, params);
	}
}
